package com.data.modules.smzh.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.common.api.vo.Result;
import com.data.common.util.JwtIOTAuthUtil;
import com.data.modules.smzh.entity.Building;
import com.data.modules.smzh.entity.SmDeviceLogs;
import com.data.modules.smzh.mapper.BuildingMapper;
import com.data.modules.smzh.service.IBuildingService;
import com.data.modules.smzh.service.IFacedeviceService;
import com.data.modules.smzh.vo.BuildReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description: 建筑资产表
 * @Author: data-boot
 * @Date:   2022-11-26
 * @Version: V1.0
 */
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements IBuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private IFacedeviceService facedeviceService;

    @Override
    public List<Building> queryBuildUnitList(LambdaQueryWrapper<Building> wrapper) {

        return buildingMapper.selectList(wrapper);
    }

    @Override
    public List<Building> queryBuildByProject(List<String> projectlist,String model) {

        return  buildingMapper.selectBuildByProjectList(projectlist,model);
    }

    @Override
    public List<String> queryDeviceInfoByProjectId(String projectId) {
        return buildingMapper.selectDeviceInfoByProjectId(projectId);
    }

    @Override
    public IPage<Building> PageList(Page<Building> page, Building queryWrapper) {
        return buildingMapper.queryPageList(page,queryWrapper);
    }

    @Override
    public Result<?> queryBuildInfo(BuildReqVo buildReqVo, HttpServletRequest req) {

        String appId = JwtIOTAuthUtil.getTokenByRequest(req);
        String reaAppId = "";
        if(ObjectUtil.isEmpty(appId)){
            return Result.error(10001,"无效token");
        }else{

            reaAppId = facedeviceService.queryDevelAppIdByToken(appId);

            List<Building> list = buildingMapper.searchBuildInfo(buildReqVo,reaAppId);
            return Result.ok(0,list);
        }
    }

    @Override
    public Building queryInfoByAppRegistId(String userId) {

        return buildingMapper.searchByAppRegistId(userId);
    }

    @Override
    public Result<?> searchVisitorInfo(SmDeviceLogs smDeviceLogs, HttpServletRequest req) {

        String appId = JwtIOTAuthUtil.getTokenByRequest(req);
        String reaAppId = "";
        if(ObjectUtil.isEmpty(appId)){
            return Result.error(10001,"无效token");
        }else{

            List<Map> list = buildingMapper.queryVisitorInfo(smDeviceLogs);
            return Result.ok(0,list);
        }
    }

    @Override
    public List<Building> queryData(String name) {

        return buildingMapper.searchBuildData(name);
    }
}
